package com.w2e.travel.mapper;

import com.w2e.travel.entity.Contents;
import com.w2e.travel.entity.Travelogue;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author yyyl
 * @since 2024-06-29
 */
public interface TravelogueMapper extends BaseMapper<Travelogue> {

    @Select("<script>" +
            "SELECT * FROM travelogue t " +
            "<where> " +
            "is_draft = 0" +
            "<if test='title != null'> " +
            "and title LIKE CONCAT('%', #{title}, '%') " +
            "</if>" +
            "</where>" +
            "</script>")
    @ResultMap("rm1")
    List<Travelogue> selectLikeTitle(@Param("title") String title);

    @Select("SELECT * FROM travelogue WHERE travelogue_id = #{id}")
    @Results(id = "rm1", value = {
            @Result(property = "travelogueId", column = "travelogue_id"),
            @Result(column = "travelogue_id", property = "contentsList",
                    many = @Many(select = "selectContentsByTid"))
    })
    Travelogue selectById(Integer id);

    @Select("SELECT * FROM contents WHERE travelogue_id = #{tid} AND is_use = 1 ORDER BY content_index ASC")
    List<Contents> selectContentsByTid(Integer tid);


    @ResultMap("rm1")
    @Select("select * from travelogue t where is_draft = 0 order by created_time desc")
    List<Travelogue> selectNewAndHot();
}
